﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;

public class Level : MonoBehaviour {

    protected List< List<Vector2> > spawn;

    protected int spawnIndex;
    protected int waveIndex;

    public Transform spawnRoot;
    protected Transform[] spawnPoints;


    [SerializeField]
    protected Transform placementRoot;

    [SerializeField]
    protected List<Level> unlockOnWin;
    public List<Level> UnlockOnWin
    {
        get { return unlockOnWin; }
        set { unlockOnWin = value; }
    }


    public Light hoverLight;
    public Light availableLight;

    [SerializeField]
    private int xpReward;
    public int XpReward
    {
        get { return xpReward; }
        set { xpReward = value; }
    }

    [SerializeField]
    protected int id;
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    [SerializeField]
    protected bool available;
    public bool Available
    {
        get { return available; }
        set 
        { 
            available = value;
            availableLight.enabled = value;
        }
    }

    [SerializeField]
    protected Rect border;
    public Rect Border
    {
        get { return border; }
        set { border = value; }
    }

    [SerializeField]
    protected Transform[] cameraPath;
    public Transform[] CameraPath
    {
        get { return cameraPath; }
        set { cameraPath = value; }
    }

    [SerializeField]
    protected GameObject[] path;
    public GameObject[] Path
    {
        get { return path; }
        set { path = value; }
    }

    public string filename;
    public string Filename
    {
        get { return filename; }
        set { filename = value; }
    }
    

	// Use this for initialization
    protected virtual void Start()
    {
        spawn = new List<List<Vector2>>();
        spawn.ForEach(s => s = new List<Vector2>());
        spawn = LevelLoader.load(Filename + ".xml");

        spawnIndex = -1;
        waveIndex = 0;
        //Select all the possible spawn points
        spawnPoints = new Transform[spawnRoot.childCount];
        int index = 0;
        foreach (Transform selectedSpawn in spawnRoot)
        {
            spawnPoints[index] = selectedSpawn;
            index++;
        }

        EventManager<EndLevelStat>.AddListener("end level", onLevelEnd);
        EventManager<Level>.AddListener("set level", onSetLevel);
	}
	
	// Update is called once per frame
    protected virtual void Update()
    {
	}

    public void showPlacementPlace(bool show)
    {
        //Enable the placement place render
        foreach (Transform selectedPlane in placementRoot)
        {
            selectedPlane.gameObject.SetActive(show);
        }
    }

    public GameObject spawnNewEnnemy(GameObject enemy)
    {
        GameObject newEnnemy = (GameObject)Instantiate(enemy, spawnPoints[0].transform.position, Quaternion.identity);
        newEnnemy.SendMessage("setPath", path);
        return newEnnemy;
    }

    public Vector2 getNextSpawn()
    {
        spawnIndex++;
        return spawn[waveIndex][spawnIndex];
    }

    public bool levelIsOver()
    {
        return (waveIndex + 1 == spawn.Count);
    }

    public bool waveIsOver()
    {
        return (spawnIndex +1 == spawn[waveIndex].Count);
    }
    
    public void setNextWave()
    {
        waveIndex++;
        spawnIndex = -1;
    }

    private void onLevelEnd(EndLevelStat stat)
    {
        raz();
    }

    public void hoverOn()
    {
        hoverLight.enabled = true;
    }

    public void hoverOff()
    {
        hoverLight.enabled = false;
    }
        
    public void raz()
    {
        waveIndex = 0;
        spawnIndex = -1;
        showPlacementPlace(false);
        availableLight.enabled = true;
    }

    public int getTotalEnemyNumber()
    {
        int sum = 0;
        for (int x = 0; x < spawn.Count; ++x)
        {
            for (int y = 0; y < spawn[x].Count; ++y)
            {
                sum += (int)spawn[x][y].y;
            }
        }
        return sum;
    }

    private void onSetLevel(Level level)
    {
        availableLight.enabled = false;
    }
}
